package com.l024.easysecurity.modules.sys.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.l024.easysecurity.common.page.PageData;
import com.l024.easysecurity.common.page.PageParams;
import com.l024.easysecurity.common.utils.R;
import com.l024.easysecurity.modules.sys.entity.SysMenu;
import com.l024.easysecurity.modules.sys.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/menu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;
    /**
     * 分页条件查询菜单列表
     */
    @PostMapping("/list")
   public R selectPageSysMenu(@RequestBody JSONObject param){
       int pageSize = 10;
       int pageNum = 1;
       SysMenu sysMenu = new SysMenu();
       if(param!=null){
           if(param.containsKey("pageSize")){
               pageSize = param.getInteger("pageSize");
           }
           if(param.containsKey("pageNum")){
               pageNum = param.getInteger("pageNum");
           }
           if(param.containsKey("menu")){
               JSONObject menu = param.getJSONObject("menu");
               if(menu!=null){
                   sysMenu = JSONObject.toJavaObject(menu,SysMenu.class);
               }
           }
       }
       PageData<SysMenu> sysMenuPageData = sysMenuService.selectPageSysMenu(sysMenu, PageParams.build(pageSize, pageNum));
       return new R().put("data",sysMenuPageData);
   }

    /**
     * 添加菜单
     */
    @PostMapping("/add")
    public R addSysMenu(@RequestBody SysMenu sysMenu){
        return sysMenuService.addSysMenu(sysMenu);
    }

    /**
     * 删除菜单
     */
    @GetMapping("/del/{menuId}")
    public R delSysMenu(@PathVariable("menuId") long menuId){
        return sysMenuService.delSysMenu(menuId);
    }

    /**
     * 更新菜单
     */
    @PostMapping("/update")
    public R updateMenu(@RequestBody SysMenu sysMenu){
        return sysMenuService.updateMenu(sysMenu);
    }
}
